//
//  AppDelegate+Service.m
//  智能备考学习
//
//  Created by 盛超 on 2018/1/8.
//  Copyright © 2018年 盛超. All rights reserved.
//

#import "AppDelegate+Service.h"
#import "XMLDictionary.h"

@implementation AppDelegate (Service)

- (void)setupBugly {
    BuglyConfig * config = [[BuglyConfig alloc] init];
    
    config.debugMode = YES;
    
    config.blockMonitorEnable = YES;
    
    config.blockMonitorTimeout = 1.5;
    
    config.channel = @"Bugly";
    
    config.delegate = self;
    
    [Bugly startWithAppId:BUGLYAPPID
#if DEBUG
        developmentDevice:YES
#endif
                   config:config];
    
    [Bugly setUserIdentifier:[NSString stringWithFormat:@"User: %@", [UIDevice currentDevice].name]];
    
    [Bugly setUserValue:[NSProcessInfo processInfo].processName forKey:@"Process"];
    
    [self performSelectorInBackground:@selector(testLogOnBackground) withObject:nil];
}

- (void)testLogOnBackground {
    int cnt = 0;
    while (1) {
        cnt++;

        switch (cnt % 5) {
            case 0:
                BLYLogError(@"Test Log Print %d", cnt);
                break;
            case 4:
                BLYLogWarn(@"Test Log Print %d", cnt);
                break;
            case 3:
                BLYLogInfo(@"Test Log Print %d", cnt);
                BLYLogv(BuglyLogLevelWarn, @"BLLogv: Test", NULL);
                break;
            case 2:
                BLYLogDebug(@"Test Log Print %d", cnt);
                BLYLog(BuglyLogLevelError, @"BLLog : %@", @"Test BLLog");
                break;
            case 1:
            default:
                BLYLogVerbose(@"Test Log Print %d", cnt);
                break;
        }

        // print log interval 1 sec.
        sleep(1);
    }
}

#pragma mark - BuglyDelegate
- (NSString *)attachmentForException:(NSException *)exception {
    NSLog(@"(%@:%d) %s %@",[[NSString stringWithUTF8String:__FILE__] lastPathComponent], __LINE__, __PRETTY_FUNCTION__,exception);
    
    return @"This is an attachment";
}

- (void)getTestLayerWithInfo:(NSDictionary*)info{
    NSString *globalGrade = [info objectForKey:@"GlobalGrade"];
    if ([globalGrade isEqualToString:@"K7"] || [globalGrade isEqualToString:@"K8"] || [globalGrade isEqualToString:@"K9"]) {//初中
        [[NSUserDefaults standardUserDefaults] setObject:@"B" forKey:@"TestLayer"];
    } else if ([globalGrade isEqualToString:@"K10"] || [globalGrade isEqualToString:@"K11"] || [globalGrade isEqualToString:@"K12"]) {//高中
        [[NSUserDefaults standardUserDefaults] setObject:@"C" forKey:@"TestLayer"];
    } else if ([globalGrade isEqualToString:@"K1"] || [globalGrade isEqualToString:@"K2"] || [globalGrade isEqualToString:@"K3"] || [globalGrade isEqualToString:@"K4"] || [globalGrade isEqualToString:@"K5"] || [globalGrade isEqualToString:@"K6"]) {//小学
        [[NSUserDefaults standardUserDefaults] setObject:@"A" forKey:@"TestLayer"];
    }
    
    NSString *SchoolID = [info objectForKey:@"SchoolID"];
    [[NSUserDefaults standardUserDefaults] setObject:SchoolID forKey:@"SchoolID"];
}

#pragma mark - 获取本应用系统IP
- (void)getSystemIP {
    NSString *url = [AddressOperater getBaseAddress];
    NSString *sysUrl = [NSString stringWithFormat:@"%@Base/WS/Service_Basic.asmx/WS_G_GetSubSystemServerInfo?sysID=P20&subjectID=S2-English",url];
    NSData *xmlData = [NSData dataWithContentsOfURL:[NSURL URLWithString:sysUrl]];
    XMLDictionaryParser *parser=[[XMLDictionaryParser alloc]init];
    NSDictionary *dic=[parser dictionaryWithData:xmlData];
    NSArray *array = dic[@"string"];
    if(array.count>0) {
        NSString *systemIP = array.lastObject;
        NSLog(@"systemIP=%@",systemIP);
        KUserDefaults(systemIP, SystemIP);
        //KUserDefaults(@"http://172.16.41.235:2016/",SystemIP);
    }
}

#pragma mark - 得到图片的IP地址
- (void)getImgIP {
}

#pragma mark - 判断是否在登录页面
- (BOOL)isLoginNavRoot
{
    NSString * isLogin = KGetUserDefaults(BKIsLogin);
    if ([isLogin isEqualToString:@"0"]) {
        return YES;
    }
    return NO;
}

-(UIViewController *)currentViewController
{
    
    UIViewController * currVC = nil;
    UIViewController * Rootvc = self.window.rootViewController ;
    do {
        if ([Rootvc isKindOfClass:[UINavigationController class]]) {
            UINavigationController * nav = (UINavigationController *)Rootvc;
            UIViewController * v = [nav.viewControllers lastObject];
            currVC = v;
            Rootvc = v.presentedViewController;
            continue;
        }else if([Rootvc isKindOfClass:[UITabBarController class]]){
            UITabBarController * tabVC = (UITabBarController *)Rootvc;
            currVC = tabVC;
            Rootvc = [tabVC.viewControllers objectAtIndex:tabVC.selectedIndex];
            continue;
        }
        
    } while (Rootvc!=nil);
    
    
    return currVC;
}


- (void)scrollAppearanceFit {
    
    UITableView.appearance.tableFooterView = UIView.new;
    
    if (@available(ios 11.0,*)) {
        UIScrollView.appearance.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        UITableView.appearance.estimatedRowHeight = 0;
        UITableView.appearance.estimatedSectionFooterHeight = 0;
        UITableView.appearance.estimatedSectionHeaderHeight = 0;
    }
}
@end
